/*
Copyright 2023 Sangfor Technologies Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package models

// This file was generated by the swagger tool.
// Editing this file might prove futile when you re-run the swagger generate command

import (
	"context"

	"github.com/go-openapi/errors"
	"github.com/go-openapi/strfmt"
	"github.com/go-openapi/swag"
	"github.com/go-openapi/validate"
)

// FaultCreate A fault to be injected.
//
// swagger:model FaultCreate
type FaultCreate struct {

	// Fault domain for a fault.
	// Required: true
	// Max Length: 64
	// Min Length: 2
	Domain *string `json:"domain"`

	// Fault inject running environment.
	// Required: true
	// Max Length: 64
	// Min Length: 2
	Env *string `json:"env"`

	// Fault type to inject
	// Required: true
	// Max Length: 128
	// Min Length: 2
	// Pattern: ^[a-z0-9-]*$
	FaultType *string `json:"fault-type"`

	// Parameters for the fault, a collection of key-value pairs. Each fault type has a specific group of valid keys.
	//
	Params map[string]*string `json:"params,omitempty"`

	// The inject fault auto remove time
	// Max Length: 64
	// Min Length: 2
	// Pattern: ^(?:(\d+d):)?(?:(\d+h):)?(?:(\d+m):)?(?:(\d+s))?$
	Timeout string `json:"timeout,omitempty"`
}

// Validate validates this fault create
func (m *FaultCreate) Validate(formats strfmt.Registry) error {
	var res []error

	if err := m.validateDomain(formats); err != nil {
		res = append(res, err)
	}

	if err := m.validateEnv(formats); err != nil {
		res = append(res, err)
	}

	if err := m.validateFaultType(formats); err != nil {
		res = append(res, err)
	}

	if err := m.validateParams(formats); err != nil {
		res = append(res, err)
	}

	if err := m.validateTimeout(formats); err != nil {
		res = append(res, err)
	}

	if len(res) > 0 {
		return errors.CompositeValidationError(res...)
	}
	return nil
}

func (m *FaultCreate) validateDomain(formats strfmt.Registry) error {

	if err := validate.Required("domain", "body", m.Domain); err != nil {
		return err
	}

	if err := validate.MinLength("domain", "body", *m.Domain, 2); err != nil {
		return err
	}

	if err := validate.MaxLength("domain", "body", *m.Domain, 64); err != nil {
		return err
	}

	return nil
}

func (m *FaultCreate) validateEnv(formats strfmt.Registry) error {

	if err := validate.Required("env", "body", m.Env); err != nil {
		return err
	}

	if err := validate.MinLength("env", "body", *m.Env, 2); err != nil {
		return err
	}

	if err := validate.MaxLength("env", "body", *m.Env, 64); err != nil {
		return err
	}

	return nil
}

func (m *FaultCreate) validateFaultType(formats strfmt.Registry) error {

	if err := validate.Required("fault-type", "body", m.FaultType); err != nil {
		return err
	}

	if err := validate.MinLength("fault-type", "body", *m.FaultType, 2); err != nil {
		return err
	}

	if err := validate.MaxLength("fault-type", "body", *m.FaultType, 128); err != nil {
		return err
	}

	if err := validate.Pattern("fault-type", "body", *m.FaultType, `^[a-z0-9-]*$`); err != nil {
		return err
	}

	return nil
}

func (m *FaultCreate) validateParams(formats strfmt.Registry) error {
	if swag.IsZero(m.Params) { // not required
		return nil
	}

	for k := range m.Params {

		if swag.IsZero(m.Params[k]) { // not required
			continue
		}

		if err := validate.MinLength("params"+"."+k, "body", *m.Params[k], 0); err != nil {
			return err
		}

		if err := validate.MaxLength("params"+"."+k, "body", *m.Params[k], 1024); err != nil {
			return err
		}

	}

	return nil
}

func (m *FaultCreate) validateTimeout(formats strfmt.Registry) error {
	if swag.IsZero(m.Timeout) { // not required
		return nil
	}

	if err := validate.MinLength("timeout", "body", m.Timeout, 2); err != nil {
		return err
	}

	if err := validate.MaxLength("timeout", "body", m.Timeout, 64); err != nil {
		return err
	}

	if err := validate.Pattern("timeout", "body", m.Timeout, `^(?:(\d+d):)?(?:(\d+h):)?(?:(\d+m):)?(?:(\d+s))?$`); err != nil {
		return err
	}

	return nil
}

// ContextValidate validates this fault create based on context it is used
func (m *FaultCreate) ContextValidate(ctx context.Context, formats strfmt.Registry) error {
	return nil
}

// MarshalBinary interface implementation
func (m *FaultCreate) MarshalBinary() ([]byte, error) {
	if m == nil {
		return nil, nil
	}
	return swag.WriteJSON(m)
}

// UnmarshalBinary interface implementation
func (m *FaultCreate) UnmarshalBinary(b []byte) error {
	var res FaultCreate
	if err := swag.ReadJSON(b, &res); err != nil {
		return err
	}
	*m = res
	return nil
}
